const { read, write } = require('../lib/read&write')

const CODEURL = '../data/confirmCodes.json'
function setCode(code) {
  const __Code = read(CODEURL)
  __Code.push(code)
  write(__Code, CODEURL)
}

function editCode(code) {
  const __Code = read(CODEURL)
  __Code.splice(
    __Code.findIndex((item) => item.account == code.id),
    1,
    code,
  )
  write(__Code, CODEURL)
}

function deleteCode(codeId) {
  const __Code = read(CODEURL)
  let targetCode = searchCode(codeId)
  __Code.splice(
    __Code.findIndex((item) => item.account == targetCode.id),
    1
  )
  write(__Code, CODEURL)
}

function searchCode(codeId) {
  const __Code = read(CODEURL)
  return __Code.find((item) => item.account === codeId)
}

module.exports = {
  searchCode,
  editCode,
  setCode,
  // 以下未用
  deleteCode,
}
